package com.lazy.mongodemo.query;


import java.util.Date;

import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lazy.lib.util.DateUtil;
import com.mongodb.Block;

public class PrintBlock<T> implements Block<T> {
    protected static final Logger logger = LoggerFactory.getLogger(PrintBlock.class);
    
    int count = 0;
    public PrintBlock(int count){
        this.count = count;
    }
    
    public int getCount(){
        return count;
    }
    @Override
    public void apply(T t){
        count++;
        if (t instanceof Document) {
            Document document = (Document)t;
            Date posTime = (Date)document.get("posTime");
            logger.info("{}, {}", document.toJson(), DateUtil.formatDate(posTime, "yyyy-MM-dd HH:mm:ss"));
        } else if (t instanceof String) {
            logger.info("{}", t);
        }
    }
}
